//
//  CoverViewController.m
//  YXFinance
//
//  Created by uplooking on 2018/10/26.
//  Copyright © 2018年 CRLong. All rights reserved.
//

#import "CoverViewController.h"

@interface CoverViewController ()
{
    
    UIImageView * imageView;
    UILabel * label;
    NSTimer * timer;
    NSInteger count;
}

@end

@implementation CoverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self getImageData];
}


- (void)initView{
    
    imageView = [[UIImageView alloc]init];
    [imageView setUserInteractionEnabled:YES];
    [self.view addSubview:imageView];
    [imageView setImage:[UIImage imageNamed:@"guanggao"]];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    label=[[UILabel alloc]init];
    [imageView addSubview:label];
    [label setText:@"3 跳过"];
    [label setBackgroundColor:HexRGBA(0xa4a4a4, 0.5)];
    [label setFont:[UIFont systemFontOfSize:11 * kAutoScanleX]];
    [label setTextColor:HexRGBA(0xffffff, 1)];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label.layer setMasksToBounds:YES];
    [label.layer setCornerRadius:10 * kAutoScanleX];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageView).offset(30 * kAutoScanleX);
        make.right.equalTo(imageView).offset(-15 * kAutoScanleX);
        make.size.mas_equalTo(CGSizeMake(50 * kAutoScanleX, 20 * kAutoScanleX));
    }];
    
  
    UIButton * button = [[UIButton alloc]init];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(label);
    }];

    
}


#pragma mark-- 跳过
-(void)buttonAction:(UIButton *)sender{
    
    [timer invalidate];
    
    BaseNavigationViewController *_mainNavigation = [[BaseNavigationViewController alloc] initWithRootViewController:[BaseTabBarViewController shareInstance]];
    [_mainNavigation.navigationBar setHidden:YES];
    [ApplicationDelegate.window setRootViewController:_mainNavigation];
    
}



#pragma mark--定时器
- (void)startTimer{
    
    count = 3;
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(reduceTime:) userInfo:nil repeats:YES];
    
    
}

-(void)reduceTime:(NSTimer *)_timer{
    
    count--;
    
    count == 0 ? [self labelNormal] : [self labelSelected];
    
}
-(void)labelNormal{
    
    [self buttonAction:nil];
    
}

-(void)labelSelected{
    
    [label setText:[NSString stringWithFormat:@"%ld 跳过",count]];
    
}

- (void)getImageData{
    
    if (UserCenterHelper.checkNetWork) {
        
        [self startTimer];
        
        
    }else{
        
        [HttpRequestHelper requestApiConfigureBlock:^(NSString *msg, ConfigureModel *model) {
        
            if (REQUEST_SUCCESS) {
                
                [imageView sd_setImageWithURL:[NSURL URLWithString:model.startup_image_url] placeholderImage:[UIImage imageNamed:@"guanggao"]];
                [self startTimer];
    
            }
            
        }];

    }
    
    
}


@end
